package main

import (
	"net/http"
	"github.com/gorilla/websocket"
	"websocket/impl"
	"time"
)

var(
	upgrader = websocket.Upgrader{
		// 允许跨域调用
		CheckOrigin: func(r *http.Request) bool {

		return true
		},
	}
)

func wsHandler(writer http.ResponseWriter, request *http.Request){
	var(
		wsConn *websocket.Conn
		err error
		conn *impl.Connection
		data []byte
	)
	if wsConn, err = upgrader.Upgrade(writer, request, nil); err != nil{
		return
	}

	// websocket.Conn
    if conn, err = impl.InitConnection(wsConn); err != nil {
		goto ERR
	}

    go func() {
    	var(
    		err error
		)
    	for{
			if err = conn.WriteMessage([]byte("heartbeat")); err != nil {
				return
			}
			time.Sleep(1 * time.Second)
		}

	}()

	for{
		if data, err = conn.ReadMessage(); err != nil{
			goto ERR
		}
		if err = conn.WriteMessage(data); err != nil{
			goto ERR
		}
	}

	ERR:
		conn.Close()
}

func main(){
	http.HandleFunc("/ws", wsHandler)
	http.ListenAndServe(":8888", nil)
}